package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @Author : wangjie
* @CreateDate : 2020/3/29 17:26
* @Version : 1.0
* @Description : 检查组前端控制器
*/
@RestController
@RequestMapping("checkgroup")
@Slf4j
public class CheckGroupController {

    //依赖注入
    @Reference
    private CheckGroupService checkGroupService;

    /**
     * 检查组分页
     * @param pageBean(当前页,每页显示条数,查询条件)
     * @return
     */
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        PageResult pageResult=null;
        try {
            //调用service 方法
            pageResult= checkGroupService.pageQuery(pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            //查询失败 返回空值
            pageResult=new PageResult(0l,null);
        }
        return pageResult;
    }

    /**
     * 添加检查组
     * @param checkGroup  检查组基本信息
     * @param checkItemIds 选中检查项列表ID数组
     * @return
     */
    @RequestMapping("add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkItemIds){
        try {
            //调用service 方法
            checkGroupService.add(checkGroup,checkItemIds);
            //返回成功结果
            return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //返回失败结果
            return new Result(false,MessageConst.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 检查组基本数据回显
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            CheckGroup checkGroup= checkGroupService.findById(id);
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     *  查询所有的检查组
     * @return
     */
    @RequestMapping("findAll")
    public Result findAll(){
        try {
            List<CheckGroup> list= checkGroupService.findAll();
            return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.QUERY_CHECKGROUP_FAIL);
        }
    }

    /**
     * 回显选中检查项
     * @param id
     * @return
     */
    @RequestMapping("findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> ids= checkGroupService.findCheckItemIdsByCheckGroupId(id);
            return new Result(true,MessageConst.ACTION_SUCCESS,ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.ACTION_FAIL);
        }
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @RequestMapping("edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        try {
            checkGroupService.edit(checkGroup,checkitemIds);
            return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.EDIT_CHECKGROUP_FAIL);
        }
    }

    /**
     * 基于id删除检查组
     * @param id
     * @return
     */
    @RequestMapping("deleteById")
    public Result deleteById(Integer id){
        try {
            checkGroupService.deleteById(id);
            return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConst.DELETE_CHECKGROUP_FAIL);
        }
    }
}
